package com.chengcheng.tuan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.chengcheng.tuan.R;
import com.chengcheng.tuan.R.id;
import com.chengcheng.tuan.R.layout;
import com.chengcheng.tuan.activity.DetailActivity;
import com.chengcheng.tuan.activity.ListActivity;

public class MianFragment extends Fragment {
	private ListView listview;
	private ViewGroup header;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		listview = (ListView) getView().findViewById(R.id.list);
		header = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.listheader_index, null);
		listview.addHeaderView(header);
		listview.setAdapter(new BaseAdapter() {
			public View getView(int arg0, View arg1, ViewGroup arg2) {
				if (arg1 != null) {
					return arg1;
				} else {
					return getActivity().getLayoutInflater().inflate(R.layout.listitem_hotel_poi, null);
				}
			}

			public long getItemId(int arg0) {
				return arg0;
			}

			public Object getItem(int arg0) {
				return arg0;
			}

			public int getCount() {
				return 20;
			}
		});
		listview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (arg2 > 0) {
					Intent intent = new Intent(getActivity(), DetailActivity.class);
					startActivity(intent);
				}
			}

		});
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				LinearLayout layout = (LinearLayout) header.getChildAt(0);
				for (int i = 0; i < layout.getChildCount(); i++) {
					View v = layout.getChildAt(i);
					ViewGroup.LayoutParams lp = v.getLayoutParams();
					lp.width = getActivity().getWindow().getDecorView().getWidth() / 4;
					lp.height = lp.width;
					v.setLayoutParams(lp);
					v.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) {
							Intent intent = new Intent(getActivity(), ListActivity.class);
							startActivity(intent);

						}
					});
				}
				layout = (LinearLayout) header.getChildAt(1);
				for (int i = 0; i < layout.getChildCount(); i++) {
					View v = layout.getChildAt(i);
					ViewGroup.LayoutParams lp = v.getLayoutParams();
					lp.width = layout.getMeasuredWidth() / 4;
					lp.height = layout.getMeasuredWidth() / 4;
					v.setLayoutParams(lp);
					v.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) {
							Intent intent = new Intent(getActivity(), ListActivity.class);
							startActivity(intent);

						}
					});
				}

			}
		}, 200);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_main, null);
	}
}
